//
//  BaseCollection.m
//  SelfDriving
//
//  Created by apple on 16/2/26.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "BaseCollection.h"

@implementation BaseCollection
{    int pageindex;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    
    
    [self setupRefresh];
    
    
}

-(void)Refresh{
    [self headerRereshing];
    [self.header beginRefreshing];
}
/**
 *  集成刷新控件
 */
- (void)setupRefresh
{
    if (self.header) {
        [self headerRereshing];
        [self.header beginRefreshing];
        
    }else{
        self.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self headerRereshing];
        }];
        ((MJRefreshNormalHeader*)self.header).lastUpdatedTimeLabel.hidden = YES;
        self.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [self footerRereshing];
        }];
        [self.header beginRefreshing];
    }
}

#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        pageindex = 1;
        [self.bdelegate initData:pageindex];
        [self reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.header endRefreshing];
    });
}
- (void)footerRereshing
{
    // 2.2秒后刷新表格UI 多线程
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        pageindex ++;
        [self.bdelegate initData:pageindex];
        [self reloadData];
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.footer endRefreshing];
    });
}
//无参数提示
-(UIView *)notData{
    _noDataLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y+self.frame.size.height/2-15, self.frame.size.width, 30)];
    _noDataLabel.text = @"暂无数据";
    _noDataLabel.textColor = UIColorFromRGB(0x666666);
    _noDataLabel.textAlignment = NSTextAlignmentCenter;
    _noDataLabel.font = [UIFont systemFontOfSize:15];
    return _noDataLabel;
}
//处理返回
-(NSMutableArray *)result:(NSMutableArray *)rsdata olddata:(NSMutableArray *)olddata index:(int)index{
    //    self.hidden = YES;
    if (index == 1) {
        [olddata removeAllObjects];
    }
    if ([rsdata count] > 0) {
        //        self.hidden = NO;
        [olddata addObjectsFromArray:rsdata];
    }
    if (index == 1 && [rsdata count] <= 0) {
        [self.footer removeFromSuperview];
        [olddata removeAllObjects];
    }
    
    if ([rsdata count] >= 20) {
        self.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [self footerRereshing];
        }];
    }else if([rsdata count]==0){
        [self.footer removeFromSuperview];
    }
    return olddata;
}
 


@end
